package com.hexinfo.dmpro.monitor.service.serviceparam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hexinfo.dmpro.monitor.model.serviceparam.WXGiveAnAlarmModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.xml.bind.DatatypeConverter;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Base64;

@Service
@Slf4j
public class GiveAnAlarmService {

    private static final Logger logger = LoggerFactory.getLogger(GiveAnAlarmService.class);

    @Value("${wx.url}")
    private String wxUrl;
    @Value("${wx.name}")
    private String name;
    @Value("${wx.password}")
    private String password;

    /**
     * 告警信息平台（微信）
     *
     * @param wx
     * @return
     */
    public String giveAnAlarmWX(WXGiveAnAlarmModel wx) {
        try {
            String jsonPrarms = JSON.toJSONString(wx);
            logger.info("访问告警平台：传参：" + jsonPrarms);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();   //获取浏览器信息
            HttpPost httpPost = new HttpPost(wxUrl);
            String encoding = DatatypeConverter.printBase64Binary((name + ":" + password).getBytes("UTF-8"));  //username  password 自行修改  中间":"不可少
            httpPost.setHeader("Authorization", "Basic " + encoding);
            //httpPost.addHeader(new BasicHeader("Authorization","Basic " + Base64.encode("用户名:密码".getBytes())));
            HttpEntity entityParam = new StringEntity(jsonPrarms, ContentType.create("application/json", "UTF-8"));  //这里的“application/json” 可以更换因为本人是传的json参数所以用的这个
            httpPost.setEntity(entityParam);     //把参数添加到post请求
            HttpResponse response = httpClient.execute(httpPost);
            String string = response.toString();
            logger.info("访问告警平台，返回数据：" + string);
            StatusLine statusLine = response.getStatusLine();   //获取请求对象中的响应行对象
            int responseCode = statusLine.getStatusCode();
            if (responseCode == 200) {
                //获取响应信息
                HttpEntity entity = response.getEntity();
                InputStream input = entity.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(input,"utf-8"));
                String str1 = br.readLine();
                logger.info("访问告警平台：服务器的响应是:" + str1);
                br.close();
                input.close();
                return "1";
            } else {
                logger.info("访问告警平台：响应失败");
                return "2";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "2";
        }
    }
}